package com.zr.service.dispatch.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.dao.car.CarInfoDao;
import com.zr.dao.dispatch.DispatchAbnormalRecordDao;
import com.zr.dao.dispatch.DispatchAcceptResultDao;
import com.zr.dao.dispatch.DispatchTicketDao;
import com.zr.service.dispatch.DispatchAbnormalRecordService;
import com.zr.vo.car.CarInfo;
import com.zr.vo.dispatch.DispatchAbnormalRecord;
import com.zr.vo.dispatch.DispatchAcceptResult;
import com.zr.vo.dispatch.DispatchTicket;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
@Service
public class DispatchAbnormalRecordServiceImpl implements DispatchAbnormalRecordService {

    @Resource
    private DispatchAbnormalRecordDao dispatchAbnormalRecordDao;
    @Resource
    private CarInfoDao carInfoDao;
    @Resource
    private DispatchAcceptResultDao dispatchAcceptResultDao;
    @Resource
    private DispatchTicketDao dispatchTicketDao;

    /**
     * 获得异常列表
     * @author 杜雨萌
     * @param dispatchAbnormalRecord
     * @return
     */
    @Override
    public PageInfo<DispatchAbnormalRecord> list(DispatchAbnormalRecord dispatchAbnormalRecord) {
        PageHelper.startPage(dispatchAbnormalRecord.getPageNum(),dispatchAbnormalRecord.getPageSize());
        List<DispatchAbnormalRecord> list = dispatchAbnormalRecordDao.list(dispatchAbnormalRecord);
        return new PageInfo<DispatchAbnormalRecord>(list);
    }

    /**
     * 获得状态列表
     * @author 杜雨萌
     * @return
     */
    @Override
    public List<String> statusList() {
        return dispatchAbnormalRecordDao.statusList();
    }

    /**
     * 获得受理结果列表
     * @author 杜雨萌
     * @return
     */
    @Override
    public List<DispatchAcceptResult> acceptResultList() {
        return dispatchAcceptResultDao.list();
    }

    @Override
    public int update(DispatchAbnormalRecord dispatchAbnormalRecord) {
        dispatchAbnormalRecord.setStatus("已受理");
        String number = dispatchAbnormalRecord.getNumber();
        String ticketId = dispatchAbnormalRecord.getTicketId();
        //更改旧车和新车的状态
        if(StringUtils.hasLength(number)){
            //修改旧车，根据调度单id查车辆
            DispatchTicket dispatchTicket=dispatchTicketDao.selectCarByDispatchTicketId(ticketId);
            CarInfo carInfo = new CarInfo();
            carInfo.setNumber(dispatchTicket.getCarNumber());

            carInfo.setCarStatus("空闲");
            carInfoDao.updateCarStatusByNumber(carInfo);
            //修改新车
            CarInfo newCar = new CarInfo();
            newCar.setNumber(number);
            newCar.setCarStatus("运输中");
            carInfoDao.updateCarStatusByNumber(newCar);
            //修改调度单对应车辆车牌号
            dispatchTicket.setCarNumber(number);
            dispatchTicketDao.updateByPrimaryKeySelective(dispatchTicket);
        }
        return dispatchAbnormalRecordDao.updateByPrimaryKeySelective(dispatchAbnormalRecord);
    }

    /***
     * 获取近6个月异常占比信息
     * @return
     */
    @Override
    public Map<String,Object> getAbnormalHalfYear() {
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH,0);
        //获取查询条件中的结束时间
        Date end=calendar.getTime();
        //调整到5个月以前
        calendar.add(Calendar.MONTH,-5);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //获取查询条件中的起始时间
        Date begin=calendar.getTime();
        List<String> typeList=dispatchAbnormalRecordDao.getTypesByTime(begin,end);
        Map<String,Object> map=new HashMap();
        List<String> monthList=new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            //循环6次获取6个月份
            Integer year=calendar.get(Calendar.YEAR);
            Integer month=calendar.get(Calendar.MONTH)+1;
            String str=year+"-"+month;
            monthList.add(str);
            //每次月份+1
            calendar.add(Calendar.MONTH,1);
        };
        map.put("异常类型",monthList);
        //获取每个异常类型所对应的rlist
        for(int j=0;j<typeList.size();j++){
            Calendar calendar2 = Calendar.getInstance();
            calendar2.add(Calendar.MONTH,-5);
            String type=typeList.get(j);
            List<Integer> rlist=new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                //循环6次获取6个月份异常数据
                Integer year=calendar2.get(Calendar.YEAR);
                Integer month=calendar2.get(Calendar.MONTH)+1;
                Integer result=dispatchAbnormalRecordDao.getNumByTypeAndMonth(type,year,month);
                rlist.add(result);
                //每次月份+1
                calendar2.add(Calendar.MONTH,1);
            }
            map.put(type,rlist);
        }
        return map;
    }
    /**
     * 查询当前调度单异常信息
     * @param ticketId
     * @return
     * @author 陈鹏
     */
    @Override
    public List<DispatchAbnormalRecord> listByDispatch(String ticketId) {
        return dispatchAbnormalRecordDao.listByDispatch(ticketId);
    }
}
